package com.unittec.drainage.provider.auth.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.unittec.drainage.provider.auth.entity.SysDic;
import com.unittec.drainage.provider.auth.entity.SysDicItem;
import com.unittec.drainage.provider.auth.service.SysDicItemService;
import com.unittec.drainage.provider.auth.service.SysDicService;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.utils.ValidationUtils;
import com.unittec.drainage.provider.vo.VoCommonResult;
import com.unittec.drainage.sdk.auth.domain.SysDicItemDTO;
import com.unittec.drainage.sdk.auth.request.Req101607;
import com.unittec.drainage.sdk.auth.request.Req101608;
import com.unittec.drainage.sdk.auth.request.Req101609;
import com.unittec.drainage.sdk.auth.request.Req101610;
import com.unittec.drainage.sdk.auth.request.Req101611;
import com.unittec.drainage.sdk.auth.request.Req101612;
import com.unittec.drainage.sdk.auth.request.Req101613;
import com.unittec.drainage.sdk.auth.request.Req101615;
import com.unittec.drainage.sdk.auth.request.Req101616;
import com.unittec.drainage.sdk.auth.request.Req101617;
import com.unittec.drainage.sdk.auth.response.Res101607;
import com.unittec.drainage.sdk.auth.response.Res101608;
import com.unittec.drainage.sdk.auth.response.Res101609;
import com.unittec.drainage.sdk.auth.response.Res101610;
import com.unittec.drainage.sdk.auth.response.Res101611;
import com.unittec.drainage.sdk.auth.response.Res101612;
import com.unittec.drainage.sdk.auth.response.Res101613;
import com.unittec.drainage.sdk.auth.response.Res101615;
import com.unittec.drainage.sdk.auth.response.Res101616;
import com.unittec.drainage.sdk.auth.response.Res101617;
import com.unittec.drainage.sdk.log.enums.LogOperateEnum;
import com.unittec.drainage.sdk.log.enums.LogStatusEnum;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Paging;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;
import com.unittec.drainage.sdk.root.enums.BizCodeEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author jtq
 */
@RestController
@Api(tags = "数据字典子字典管理")
public class SysDicItemController {

    @Autowired
    SysDicItemService sysDicItemService;
    @Autowired
    SysDicService sysDicService;
    @Autowired
    Log log;

    @RequestMapping(value = "/101607", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "子字典添加", notes = "子字典添加")
    public Result<Res101607> saveDicItem(@RequestBody GatewayRequest<Req101607> req) {
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        long startLogTime = System.currentTimeMillis();
        Res101607 res101607 = new Res101607();
        log.debug(req, "saveDicItem {} ,param {}  ", "begin", JSON.toJSONString(req.getContent()));
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            SysDicItem sysDicItem = new SysDicItem();
            BeanUtils.copyProperties(req.getContent(), sysDicItem);

            VoCommonResult voCommonResult = sysDicItemService.saveDicItem(sysDicItem);
            if (voCommonResult.getIsSuccess()) {
                res101607.setId(voCommonResult.getPrimaryKey());
                bizCodeEnum = BizCode.SUCCESS;
                log.addOperateLog("101607", JSON.toJSONString(req), "子字典添加", "子字典添加成功",
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            } else {
                bizCodeEnum = BizCode.ERROR;
                log.addOperateLog("101607", JSON.toJSONString(req), "子字典添加", "子字典添加失败：" + bizCodeEnum.getCode(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.toString());
        }
        log.debug(req, "saveDicItem{}  ", "end");

        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101607);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101608", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改子字典", notes = "修改子字典")
    public Result<Res101608> updateDicItem(@RequestBody GatewayRequest<Req101608> req) {
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        long startLogTime = System.currentTimeMillis();
        log.debug(req, "updateDicItem {}  ,param {} ", "begin", JSON.toJSONString(req.getContent()));
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            SysDicItem sysDicItem = sysDicItemService.getDicItemDetail(req.getContent().getId());
            // 查询数据是否存在
            if (ObjectUtils.isNotNull(sysDicItem)) {
                VoCommonResult voCommonResult = sysDicItemService.updateDicItem(req.getContent(), sysDicItem);
                if (voCommonResult.getIsSuccess()) {
                    bizCodeEnum = BizCode.SUCCESS;
                    log.addOperateLog("101608", JSON.toJSONString(req), "修改子字典", "修改子字典成功",
                        LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
                } else {
                    bizCodeEnum = BizCode.ERROR;
                    log.addOperateLog("101608", JSON.toJSONString(req), "修改子字典", "修改子字典失败：" + bizCodeEnum.getCode(),
                        LogOperateEnum.LOG_UPDATE.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
                }
            } else {
                bizCodeEnum = BizCode.NOT_LIST;
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.toString());
        }
        log.debug(req, "updateDicItem{}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101609", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "子字典删除", notes = "子字典删除")
    public Result<Res101609> delDicItem(@RequestBody GatewayRequest<Req101609> req) {
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        long startLogTime = System.currentTimeMillis();
        log.debug(req, "delDicItem {}  ", "begin");
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            boolean ret = sysDicItemService.removeById(req.getContent().getId());
            if (ret) {
                bizCodeEnum = BizCode.SUCCESS;
                log.addOperateLog("101609", JSON.toJSONString(req), "子字典删除", "子字典删除成功",
                    LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            } else {
                bizCodeEnum = BizCode.ERROR;
                log.addOperateLog("101609", JSON.toJSONString(req), "子字典删除", "子字典删除失败：" + bizCodeEnum.getCode(),
                    LogOperateEnum.LOG_DEL.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.toString());
        }
        log.debug(req, "delDicItem {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101610", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "子字典查看", notes = "子字典查看分页列表")
    public Result<Res101610> getDicItemList(@RequestBody GatewayRequest<Req101610> req) {
        log.debug(req, "getDicItemList{} ,param {}  ", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101610 res101610 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Paging paging = new Paging();
            BeanUtils.copyProperties(req.getContent(), paging);
            IPage<Map<String, Object>> sysDicItemList = sysDicItemService.getDicItemList(paging,
                req.getContent().getDictId(), req.getContent().getDictCode(), req.getContent().getSelectOne());
            res101610 = new Res101610();
            res101610.setPage(sysDicItemList);
            bizCodeEnum = BizCode.SUCCESS;

        } catch (Exception e) {
            log.error(req, e.toString(), e);
        }
        log.debug(req, "getDicItemList {}  ", "end");

        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101610);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101611", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "子字典查询详情信息", notes = "子字典查询详情信息")
    public Result<Res101611> getDicItemDetail(@RequestBody GatewayRequest<Req101611> req) {
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        log.debug(req, "getDicItemDetail {} ,param {}  ", "begin", JSON.toJSONString(req.getContent()));
        Res101611 res101611 = new Res101611();
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            SysDicItem sysDicItem = sysDicItemService.getDicItemDetail(req.getContent().getId());
            if (ObjectUtils.isNotNull(sysDicItem)) {
                BeanUtils.copyProperties(sysDicItem, res101611);
                bizCodeEnum = BizCode.SUCCESS;
            } else {
                bizCodeEnum = BizCode.ERROR;
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
        }
        log.debug(req, "getDicItemDetail {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101611);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101612", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "子字典查看不分页", notes = "子字典查看分页列表")
    public Result<Res101612> getDicItemListNoPage(@RequestBody GatewayRequest<Req101612> req) {
        log.debug(req, "getDicItemListNoPage{} ,param {} ", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101612 res101612 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            List<Map<String, Object>> sysDicItemListNo = sysDicItemService.getDicItemListNoPage(
                req.getContent().getDictId(), req.getContent().getDictCode(), req.getContent().getSelectOne());
            res101612 = new Res101612();
            res101612.setObj(sysDicItemListNo);
            bizCodeEnum = BizCode.SUCCESS;

        } catch (Exception e) {
            log.error(req, e.toString(), e);
        }
        log.debug(req, "getDicItemListNoPage {}  ", "end");

        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101612);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101613", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据字典项值和字典编码查询字典项文本", notes = "根据字典项值和字典编码查询字典项文本")
    public Result<Res101613> getItemTextByDicCodeAndItemValue(@RequestBody GatewayRequest<Req101613> req) {
        log.debug(req, "getItemTextByDicCodeAndItemValue{} ,param {} ", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101613 res101613 = null;
        try {
            String itemText = sysDicItemService.getItemTextByDicCodeAndItemValue(req.getContent().getDicCode(),
                req.getContent().getItemValue());
            res101613 = new Res101613();
            res101613.setItemText(itemText);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.toString());
        }
        log.debug(req, "getItemTextByDicCodeAndItemValue {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101613);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101615", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "子字典唯一性校验", notes = "子字典唯一性校验")
    public Result<Res101615> itemDicUnique(@RequestBody GatewayRequest<Req101615> req) {
        log.debug(req, "itemDicUnique{} ,param {} ", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.SUCCESS;
        Res101615 res101615 = new Res101615();
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            String id = req.getContent().getId();
            SysDicItem sysDicItem = null;
            if (StringUtils.isNotEmpty(id)) {
                sysDicItem = sysDicItemService.getDicItemDetail(id);
            }

            SysDicItem sysDicItem2 = sysDicItemService.itemDicUnique(req.getContent());
            if (sysDicItem2 != null) {
                if (sysDicItem == null) {
                    res101615.setIsSuccess(false);
                } else if (!id.equals(sysDicItem2.getId())) {
                    res101615.setIsSuccess(false);
                }
            }
        } catch (Exception e) {
            log.error(req, e.toString(), e);
        }
        log.debug(req, "itemDicUnique {}  ", "end");

        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101615);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101616", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "通过字典编码批量获取字典信息", notes = "通过字典编码批量获取字典信息")
    public Result<Res101616> getDicInfoListByCodes(@RequestBody GatewayRequest<Req101616> req) {
        log.debug(req, "getDicInfoListByCodes{} ,param {}  ", "begin", JSON.toJSONString(req.getContent()));
        long startLogTime = System.currentTimeMillis();
        try {
            Map<String, List<Map<String, Object>>> map =
                sysDicItemService.getDicInfoListByCodes(req.getContent().getDicCodes());
            Res101616 res = new Res101616();
            res.setDicMapList(map);
            log.debug(req, "getDicInfoListByCodes{}  ", "end");

            return Result.success(req, res);
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, BizCode.ERROR.getCode());
        }
    }

    /**
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "/101617", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "通过字典编码获取字典信息", notes = "通过字典编码获取字典信息")
    public Result<Res101617> findDicItemListByDicCode(@RequestBody GatewayRequest<Req101617> req) {
        log.debug(req, "findDicItemListByDicCode{}  ", "begin");
        try {
            Req101617 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            Res101617 res = new Res101617();
            SysDic byDicCode = sysDicService.getByDicCode(content.getDicCode());
            if (null == byDicCode) {
                log.debug(req, "findDicItemListByDicCode{}  ", "end");
                return Result.error(req, BizCode.ERROR.getCode());
            }
            List<SysDicItemDTO> dicItemListByDicCode = sysDicItemService.findDicItemListByDicCode(content.getDicCode());
            res.setObj(dicItemListByDicCode);
            log.debug(req, "findDicItemListByDicCode{}  ", "end");
            return Result.success(req, res);
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, BizCode.ERROR.getCode());
        }
    }
}
